package com.zq.widget.inverted.game.role.decorate;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;

import com.zq.widget.inverted.game.role.hp.HP;

/**
 * Created by zhangqiang on 2018/3/29.
 */
public class HPBar implements Decorator {

    private Paint paint = new Paint();
    private RectF hpBarRectF = new RectF();
    private RectF hpFrameRectF = new RectF();
    private HP hp;
    private float height;
    private float space;

    public HPBar(float borderWidth, int color, float height, float space, HP hp) {
        this.height = height;
        this.hp = hp;
        this.space = space;
        paint.setAntiAlias(true);
        paint.setColor(color);
        paint.setStrokeWidth(borderWidth);
    }

    private static void considerBorder(RectF src,RectF target,float border){

        float width = target.width() - border;
        float height = target.height() - border;
        float left = target.left + border/2;
        float top  = target.top + border/2;
        float right = left + width;
        float bottom = top + height;
        src.set(left,top,right,bottom);
    }

    @Override
    public void onDraw(Canvas canvas, RectF roleRectF) {

        float strokeWidth = paint.getStrokeWidth();
        hpFrameRectF.set(roleRectF.left,roleRectF.top - space - height,roleRectF.right,roleRectF.top - space);
        considerBorder(hpFrameRectF,hpFrameRectF, strokeWidth);

        paint.setStyle(Paint.Style.STROKE);
        canvas.drawRect(hpFrameRectF,paint);

        considerBorder(hpBarRectF,hpFrameRectF, strokeWidth);
        computeHp();
        paint.setStyle(Paint.Style.FILL);
        canvas.drawRect(hpBarRectF,paint);
    }

    private void computeHp(){

        float factor = (float) hp.getHp() / hp.totalHp();
        float width = hpFrameRectF.width();
        float left = hpFrameRectF.left;
        float top = hpFrameRectF.top;
        float right = left + width * factor;
        float bottom = hpFrameRectF.bottom;
        this.hpBarRectF.set(left, top,right,bottom);
    }
}
